package com.lancoo.answer.view.result

import android.graphics.Typeface
import android.os.Handler
import android.view.View
import com.lancoo.answer.R
import com.lancoo.answer.databinding.EvFragmentSameResultAnswerSheetBinding
import com.lancoo.answer.model.bean.SameQuesDetail
import com.lancoo.answer.util.DoubleUtils
import com.lancoo.answer.util.ScoreSoundUtils
import com.lancoo.answer.view.base.XBaseFragment
import com.lancoo.answer.widget.CountNumberView
import org.greenrobot.eventbus.EventBus

class SameResultAnswerSheetFragment : XBaseFragment<EvFragmentSameResultAnswerSheetBinding>() {

    companion object {
        private var from: String = ""
        private var isConsolidated: Int = 0
        private var actID: String = ""
        private lateinit var sameQuesDetail: SameQuesDetail
        fun loadFragment(
            mFrom: String,
            mIsConsolidated: Int,
            mActID: String,
            mSameQuesDetail: SameQuesDetail
        ): SameResultAnswerSheetFragment? {
            val resultFragment = SameResultAnswerSheetFragment()
            from = mFrom
            isConsolidated = mIsConsolidated
            actID = mActID
            sameQuesDetail = mSameQuesDetail////传入同类题结果页面数据结构
            return resultFragment
        }
    }

    private lateinit var scoreSoundUtils: ScoreSoundUtils

    override fun initView() {
        //初始化短音频
        scoreSoundUtils = ScoreSoundUtils(requireContext())
        scoreSoundUtils!!.init()

        Handler().postDelayed({

            //显示我的准确率相关
            showScoreRateView()

            //显示答题卡
            binding.RvAnswerSheetParent.recycledViewPool.clear()
            binding.RvAnswerSheetParent.setItemViewCacheSize(0)

            val adapterParent = SameAnswerSheetParentAdapter(sameQuesDetail, 1)
            binding.RvAnswerSheetParent.adapter = adapterParent
            adapterParent.setClickParentListener(object :
                SameAnswerSheetParentAdapter.OnClickParentListener {
                override fun onClick(sortIndex: String) {
                    //跳转到同类题查看界面
                    EventBus.getDefault().post(sortIndex)
                }

            })
        }, 500)
    }

    //加载我的得分相关信息
    private fun showScoreRateView() {

        var isNotCorrectRate = false//是否是有效正确率
        var correctRateAll = 0.0
        val sameQuesSize = sameQuesDetail!!.size
        outLoop@ for (i in 0 until sameQuesSize) {
            val correctRate = sameQuesDetail!![i].TkModel.CorrectRate
            if (correctRate == -1.0) {
                isNotCorrectRate = true
                break@outLoop
            }
            correctRateAll += correctRate
        }
        if (isNotCorrectRate) {
            binding.TvMyScoreRate.text = "--"
            binding.TvMyScoreRate.typeface =
                Typeface.createFromAsset(requireContext().assets, "fonts/DIN Condensed Bold.ttf")
            binding.TvPercentSign.typeface =
                Typeface.createFromAsset(requireContext().assets, "fonts/DIN Condensed Bold.ttf")
            binding.LlPassMessage.visibility = View.GONE
        } else {
            val scoreRate = DoubleUtils.div(correctRateAll * 100, sameQuesSize.toDouble(), 2)
            val intScoreRate = scoreRate.toInt()
            //显示准确率
            if (intScoreRate > 0) {
                scoreSoundUtils.playSound(0)
            }
            binding.TvMyScoreRate.showNumberWithAnimation(
                intScoreRate.toFloat(),
                CountNumberView.INTREGEX
            );

            binding.TvMyScoreRate.typeface =
                Typeface.createFromAsset(requireContext().assets, "fonts/DIN Condensed Bold.ttf")
            binding.TvPercentSign.typeface =
                Typeface.createFromAsset(requireContext().assets, "fonts/DIN Condensed Bold.ttf")

            //显示是否巩固
            if (from == "同类题训练") {
                binding.LlPassMessage.visibility = View.VISIBLE
                if (isConsolidated == 0) {
                    binding.IvPassPicture.setImageResource(R.mipmap.ev_icon_get_no)
                } else {
                    binding.IvPassPicture.setImageResource(R.mipmap.ev_icon_get_yes)
                }
                //显示提示语
                var message1 = ""
                var message2 = ""
                if (intScoreRate == 100) {
                    message1 = "祝贺你！"
                    message2 = "错题轻松击破！"
                } else if (intScoreRate in 85..99) {
                    message1 = "优秀！"
                    message2 = "成功就在眼前！"
                } else if (intScoreRate in 60..84) {
                    message1 = "加油！"
                    message2 = "争取下次有更出色表现！"
                } else {
                    message1 = "别泄气！"
                    message2 = "努力会给你更多惊喜！"
                }
                binding.TvPassMessage1.text = message1
                binding.TvPassMessage2.text = message2
            } else {
                binding.LlPassMessage.visibility = View.GONE
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        scoreSoundUtils.stopAllSoundPool()
    }

    override fun onPause() {
        super.onPause()
        scoreSoundUtils.stopAllSoundPool()
    }

}